package threading;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

public class ThreadDemo18 {
    public static void main(String[] args) {
        BlockingQueue<Integer> blockingQueue = new LinkedBlockingDeque<>();

        //消费者
        Thread t1 = new Thread(()->{
            while(true){
                int value = 0;
                try {
                    value = blockingQueue.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("消费元素:" + value);
            }
        });
        t1.start();


        //生产者
        Thread t2 = new Thread(()->{
            int value = 0;
            while(true){
                System.out.println("生产元素:"+ value);
                try {
                    blockingQueue.put(value);
                    value++;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                   e.printStackTrace();
                }
            }
        });
        t2.start();
        //生产者每隔1s生产一个元素
        //消费者不受限制 直接消费
    }
}
